package com.zhangyu.shadow.core.delegate.web;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zhangyu.shadow.core.delegate.IPageLoadListener;
import com.zhangyu.shadow.core.delegate.web.chromeclient.WebChromeClientImpl;
import com.zhangyu.shadow.core.delegate.web.client.WebViewClientImpl;
import com.zhangyu.shadow.core.delegate.web.route.RouteKey;
import com.zhangyu.shadow.core.delegate.web.route.Router;

/**
 * @author zhangyu
 * @date 2017/11/13
 */

public final class WebDelegateImpl extends WebDelegate {
    private IPageLoadListener mIPageLoadListener;

    public static WebDelegateImpl create(String url) {
        Bundle bundle = new Bundle();
        bundle.putString(RouteKey.URL.name(), url);
        final WebDelegateImpl webDelegate = new WebDelegateImpl();
        webDelegate.setArguments(bundle);
        return webDelegate;
    }

    public void setIPageLoadListener(IPageLoadListener IPageLoadListener) {
        mIPageLoadListener = IPageLoadListener;
    }

    @Override
    public Object setLayout() {
        return getWebView();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (getUrl() != null) {
            //原生模拟webView跳转
            Router.getInstance().loadPage(this, getUrl());
        }
    }

    @Override
    public IWebViewInitialize setInitialize() {
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public WebView initWebView(WebView webView) {
        return new WebViewInitializer().createWebView(webView);
    }

    @Override
    public WebViewClient initWebViewClient() {
        WebViewClientImpl client = new WebViewClientImpl(this);
        client.setIPageLoadListener(mIPageLoadListener);
        return client;
    }

    @Override
    public WebChromeClient initWebChromeClient() {
        return new WebChromeClientImpl();
    }
}
